// const AUTH = require('utils/auth')
const WXAPI = require('api/api')
import config from './config'

App({
  /**
   * 全局变量
   */
  globalData: {
    userInfo: wx.getStorageSync('userInfo'), // 当前登陆用户信息
    isConnected: true,
    dictTree: wx.getStorageSync('dictTree'),
    bannerOptions: wx.getStorageSync('bannerOptions')
  },
  onLaunch: function () {
    // const subDomain = wx.getExtConfigSync().subDomain
    // if (subDomain) {} else {

    // }

    const that = this;
    // 检测新版本
    const updateManager = wx.getUpdateManager()
    updateManager.onUpdateReady(function () {
      wx.showModal({
        title: '更新提示',
        content: '新版本已经准备好，是否重启应用？',
        success(res) {
          if (res.confirm) {
            // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
            updateManager.applyUpdate()
          }
        }
      })
    })
    /**
     * 初次加载判断网络情况
     * 无网络状态下根据实际情况进行调整
     */
    wx.getNetworkType({
      success(res) {
        const networkType = res.networkType
        if (networkType === 'none') {
          that.globalData.isConnected = false
          wx.showToast({
            title: '当前无网络',
            icon: 'loading',
            duration: 2000
          })
        }
      }
    });
    /**
     * 监听网络状态变化
     * 可根据业务需求进行调整
     */
    wx.onNetworkStatusChange(function (res) {
      if (!res.isConnected) {
        that.globalData.isConnected = false
        wx.showToast({
          title: '网络已断开',
          icon: 'loading',
          duration: 2000
        })
      } else {
        that.globalData.isConnected = true
        wx.hideToast()
      }
    })

    // ---------------检测navbar高度
    // let menuButtonObject = wx.getMenuButtonBoundingClientRect();
    // console.log("小程序胶囊信息", menuButtonObject)
    // wx.getSystemInfo({
    //   success: res => {
    //     let statusBarHeight = res.statusBarHeight,
    //       navTop = menuButtonObject.top, //胶囊按钮与顶部的距离
    //       navHeight = statusBarHeight + menuButtonObject.height + (menuButtonObject.top - statusBarHeight) * 2; //导航高度
    //     this.globalData.navHeight = navHeight;
    //     this.globalData.navTop = navTop;
    //     this.globalData.windowHeight = res.windowHeight;
    //     this.globalData.menuButtonObject = menuButtonObject;
    //   },
    //   fail(err) {
    //     console.log(err);
    //   }
    // })
  },

  onShow(e) {

  },

  setUserInfo(obj){
    let userInfo = {
      nickname: obj.sNickname,
      headimage: obj.sHeadimgurl
    }
    wx.setStorageSync('userInfo', userInfo);
    this.globalData.userInfo = userInfo
  },
  /**
   * 授权登录
   */
  getUserInfo(e, callback) {
    let _this = this
    // wx.showLoading({
    //   title: "正在登录...",
    //   mask: true
    // });
    // 执行微信登录
    wx.login({
      success(res1) {
        let params
        if (e && e.nickname) {
          params = {
            sCode: res1.code,
            sNickname: e.nickname,
            sHeadimgBase64: e.headimage,
            sAppid:config.appId
          }
        } else {
          params = {
            sCode: res1.code,
            sAppid:config.appId
          }
        }
        WXAPI.login(params).then(async function (res) {
          // 存储token
          wx.setStorageSync('token', res.data.data.LoginToken);
          let obj = {
            sNickname:res.data.data.Claims.sNickname,
            sHeadimgurl:res.data.data.Claims.sHeadimgurl
          }
          _this.setUserInfo(obj)
          // 获取字典
          let dictRes = await WXAPI.getDictTree()
          if (dictRes.data.code == 0) {
            wx.setStorageSync('dictTree', dictRes.data.data);
            _this.globalData.dictTree = dictRes.data.data
          }
          // 获取配置项-banner
          let optionsRes = await WXAPI.getConfigOptions({})
          if (optionsRes.data.code == 0) {
            wx.setStorageSync('bannerOptions', optionsRes.data.data);
            _this.globalData.bannerOptions = optionsRes.data.data
            // wx.hideLoading();
            callback && callback()
          }

        })
      }
    })
  },

  async getDict() {

  },
  /**
   * 验证登录
   */
  checkIsLogin() {
    return wx.getStorageSync('token') != '';
  }
})